import axios from 'axios'
import { ElNotification } from 'element-plus'

const request = axios.create({
  baseURL: import.meta.env.VITE_APP_BASE_API,
  timeout: 5000,
})

// 配置请求拦截器
request.interceptors.request.use(
  (config) => {
    config.headers.token = localStorage.getItem('token')
    return config
  },
  (err) => {
    return Promise.reject(err)
  },
)

// 配置响应拦截器
request.interceptors.response.use(
  (response) => {
    return response.data
  },
  (error) => {
    switch (error.status) {
      case 404:
        ElNotification({
          title: '用户名或密码错误',
        })
    }
    return Promise.reject(error)
  },
)

export { request }
